﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace RBS.Web.Hotel.tools
{
    /// <summary>
    /// uploadAjax 的摘要说明
    /// </summary>
    public class uploadAjax : IHttpHandler
    {
         
        public void ProcessRequest(HttpContext context)
        {
                var result = "0";
                try
                {
                    if (context.Request.Files.Count > 0)
                    {
                        HttpPostedFile file = null;

                        for (int i = 0; i < context.Request.Files.Count; i++)
                        {
                            file = context.Request.Files[i];
                            if (file.ContentLength > 0)
                            {
                                var fileName = Path.GetFileName(file.FileName);
                              /*  var path = Path.Combine(<somepath>, fileName);*/
                           /*     file.SaveAs(path);*/
                                result = "1"; 
                            }
                        }    

                    }
                }
                catch { }
              context.Response.Write(result);
        }


       /* public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string action = context.Request["action"];
            if (action == "UploadImages")
            {
                UploadImages(context);
            }
        } */
        public void UploadImages(HttpContext context)
        {
            const string outpath = "UploadImages" + "\\" + "License\\";
            string physicalPath = context.Server.MapPath("~/") + outpath;
            if (!Directory.Exists(physicalPath))
            {
                Directory.CreateDirectory(physicalPath);
            } 
           // HttpFileCollection files = context.Request.Files;
             HttpFileCollection files = context.Request.Files;	
            if (files.Count > 0)
            {
                HttpPostedFile file = files[0]; 
                string tmpPath = physicalPath;
                string fileName = Path.GetFileName(file.FileName);
                try
                {
                    file.SaveAs(tmpPath + fileName);
                    context.Response.Write(@"{
                    status : 'success', 
                    msg: { 
                        Origin : '" + "/upload/" + fileName + @"',type:'add'
                    }
                }");
                }
                catch (Exception ex)
                {
                    context.Response.Write(@"{
                status : 'error',
                msg : '" + ex.Message + @"'
                }");

                }
                context.Response.End();
            }
            return;
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}